package com.ht.shop.dao.impl;

import com.ht.shop.dao.GoodsTypeDao;
import com.ht.shop.pojo.TbGoodsType;
import com.ht.shop.utils.DBUtils;
import com.ht.shop.utils.SqlUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class GoodsTypeDaoImpl implements GoodsTypeDao {
    private QueryRunner queryRunner= new QueryRunner(DBUtils.getSource());
    @Override
    public List<TbGoodsType> findAllSecondLeveType() throws SQLException {
        String sql = SqlUtils.getSql("goodstype.findallsecond");
        List<TbGoodsType> goodsList = queryRunner.query(sql, new BeanListHandler<>(TbGoodsType.class));

        return goodsList;
    }

    @Override
    public void addType(String name, String parent) throws SQLException {

        String sql = "INSERT INTO tb_goods_type (name,level,Parent) VALUES(?,0,?)";
        queryRunner.update(sql,name,parent);
    }

    @Override
    public int deleteType(String id) {
        String sql = "update tb_goods_type set status = 0 where id =?";
        try {
            return queryRunner.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<TbGoodsType> findIdByPIdAndStaus(String pid, int status) {
        String sql = "select id from tb_goods_type where Parent =? and status=?";
        List<TbGoodsType> typeList = null;
        try {
           typeList = queryRunner.query(sql, new BeanListHandler<>(TbGoodsType.class), pid, status);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return typeList;
    }
}
